// components/ad-splash/ad-splash.ts
Component({
  properties: {
    // 广告图片路径
    adImage: {
      type: String,
      value: ''
    },
    // 倒计时时间（秒）
    countdownTime: {
      type: Number,
      value: 5
    },
    // 是否显示广告
    showAd: {
      type: Boolean,
      value: false
    }
  },

  data: {
    countdown: 5,
    timer: null as any
  },

  observers: {
    'showAd': function(showAd) {
      if (showAd) {
        this.startCountdown();
        this.triggerEvent('hideTabBar');
      } else {
        this.clearTimer();
        this.triggerEvent('showTabBar');
      }
    }
  },

  lifetimes: {
    attached() {
      // 组件初始化
    },

    detached() {
      // 组件销毁时清理定时器
      this.clearTimer();
      // 显示tabBar
      this.triggerEvent('showTabBar');
    }
  },

  methods: {
    // 开始倒计时
    startCountdown() {
      this.setData({
        countdown: this.properties.countdownTime
      });
      
      this.clearTimer();
      
      this.data.timer = setInterval(() => {
        const newCountdown = this.data.countdown - 1;
        this.setData({
          countdown: newCountdown
        });
        
        if (newCountdown <= 0) {
          this.skipAd();
        }
      }, 1000);
    },

    // 清理定时器
    clearTimer() {
      if (this.data.timer) {
        clearInterval(this.data.timer);
        this.setData({
          timer: null
        });
      }
    },

    // 跳过广告
    skipAd() {
      this.clearTimer();
      this.triggerEvent('showTabBar'); // 显示tabBar
      this.triggerEvent('adSkip', {
        countdown: this.data.countdown
      });
    },

    // 点击广告图片
    onAdImageTap() {
      this.triggerEvent('adTap', {
        adImage: this.properties.adImage
      });
    }
  }
});
